/*     */ package com.cr.framework.web.domain;
/*     */ 
/*     */ import com.cr.common.utils.Arith;
/*     */ import com.cr.common.utils.IpUtils;
/*     */ import com.cr.framework.web.domain.server.Cpu;
/*     */ import com.cr.framework.web.domain.server.Jvm;
/*     */ import com.cr.framework.web.domain.server.Mem;
/*     */ import com.cr.framework.web.domain.server.Sys;
/*     */ import com.cr.framework.web.domain.server.SysFile;
/*     */ import java.net.UnknownHostException;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.Properties;
/*     */ import oshi.SystemInfo;
/*     */ import oshi.hardware.CentralProcessor;
/*     */ import oshi.hardware.GlobalMemory;
/*     */ import oshi.hardware.HardwareAbstractionLayer;
/*     */ import oshi.software.os.FileSystem;
/*     */ import oshi.software.os.OSFileStore;
/*     */ import oshi.software.os.OperatingSystem;
/*     */ import oshi.util.Util;
/*     */ 
/*     */ public class Server {
/*     */   private static final int OSHI_WAIT_SECOND = 1000;
/*     */   
/*  37 */   private Cpu cpu = new Cpu();
/*     */   
/*  42 */   private Mem mem = new Mem();
/*     */   
/*  47 */   private Jvm jvm = new Jvm();
/*     */   
/*  52 */   private Sys sys = new Sys();
/*     */   
/*  57 */   private List<SysFile> sysFiles = new LinkedList<>();
/*     */   
/*     */   public Cpu getCpu() {
/*  61 */     return this.cpu;
/*     */   }
/*     */   
/*     */   public void setCpu(Cpu cpu) {
/*  66 */     this.cpu = cpu;
/*     */   }
/*     */   
/*     */   public Mem getMem() {
/*  71 */     return this.mem;
/*     */   }
/*     */   
/*     */   public void setMem(Mem mem) {
/*  76 */     this.mem = mem;
/*     */   }
/*     */   
/*     */   public Jvm getJvm() {
/*  81 */     return this.jvm;
/*     */   }
/*     */   
/*     */   public void setJvm(Jvm jvm) {
/*  86 */     this.jvm = jvm;
/*     */   }
/*     */   
/*     */   public Sys getSys() {
/*  91 */     return this.sys;
/*     */   }
/*     */   
/*     */   public void setSys(Sys sys) {
/*  96 */     this.sys = sys;
/*     */   }
/*     */   
/*     */   public List<SysFile> getSysFiles() {
/* 101 */     return this.sysFiles;
/*     */   }
/*     */   
/*     */   public void setSysFiles(List<SysFile> sysFiles) {
/* 106 */     this.sysFiles = sysFiles;
/*     */   }
/*     */   
/*     */   public void copyTo() throws Exception {
/* 111 */     SystemInfo si = new SystemInfo();
/* 112 */     HardwareAbstractionLayer hal = si.getHardware();
/* 114 */     setCpuInfo(hal.getProcessor());
/* 116 */     setMemInfo(hal.getMemory());
/* 118 */     setSysInfo();
/* 120 */     setJvmInfo();
/* 122 */     setSysFiles(si.getOperatingSystem());
/*     */   }
/*     */   
/*     */   private void setCpuInfo(CentralProcessor processor) {
/* 131 */     long[] prevTicks = processor.getSystemCpuLoadTicks();
/* 132 */     Util.sleep(1000L);
/* 133 */     long[] ticks = processor.getSystemCpuLoadTicks();
/* 134 */     long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
/* 135 */     long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
/* 136 */     long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
/* 137 */     long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
/* 138 */     long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
/* 139 */     long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
/* 140 */     long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
/* 141 */     long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
/* 142 */     long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
/* 143 */     this.cpu.setCpuNum(processor.getLogicalProcessorCount());
/* 144 */     this.cpu.setTotal(totalCpu);
/* 145 */     this.cpu.setSys(cSys);
/* 146 */     this.cpu.setUsed(user);
/* 147 */     this.cpu.setWait(iowait);
/* 148 */     this.cpu.setFree(idle);
/*     */   }
/*     */   
/*     */   private void setMemInfo(GlobalMemory memory) {
/* 156 */     this.mem.setTotal(memory.getTotal());
/* 157 */     this.mem.setUsed(memory.getTotal() - memory.getAvailable());
/* 158 */     this.mem.setFree(memory.getAvailable());
/*     */   }
/*     */   
/*     */   private void setSysInfo() {
/* 166 */     Properties props = System.getProperties();
/* 167 */     this.sys.setComputerName(IpUtils.getHostName());
/* 168 */     this.sys.setComputerIp(IpUtils.getHostIp());
/* 169 */     this.sys.setOsName(props.getProperty("os.name"));
/* 170 */     this.sys.setOsArch(props.getProperty("os.arch"));
/* 171 */     this.sys.setUserDir(props.getProperty("user.dir"));
/*     */   }
/*     */   
/*     */   private void setJvmInfo() throws UnknownHostException {
/* 179 */     Properties props = System.getProperties();
/* 180 */     this.jvm.setTotal(Runtime.getRuntime().totalMemory());
/* 181 */     this.jvm.setMax(Runtime.getRuntime().maxMemory());
/* 182 */     this.jvm.setFree(Runtime.getRuntime().freeMemory());
/* 183 */     this.jvm.setVersion(props.getProperty("java.version"));
/* 184 */     this.jvm.setHome(props.getProperty("java.home"));
/*     */   }
/*     */   
/*     */   private void setSysFiles(OperatingSystem os) {
/* 192 */     FileSystem fileSystem = os.getFileSystem();
/* 193 */     List<OSFileStore> fsArray = fileSystem.getFileStores();
/* 194 */     for (OSFileStore fs : fsArray) {
/* 196 */       long free = fs.getUsableSpace();
/* 197 */       long total = fs.getTotalSpace();
/* 198 */       long used = total - free;
/* 199 */       SysFile sysFile = new SysFile();
/* 200 */       sysFile.setDirName(fs.getMount());
/* 201 */       sysFile.setSysTypeName(fs.getType());
/* 202 */       sysFile.setTypeName(fs.getName());
/* 203 */       sysFile.setTotal(convertFileSize(total));
/* 204 */       sysFile.setFree(convertFileSize(free));
/* 205 */       sysFile.setUsed(convertFileSize(used));
/* 206 */       sysFile.setUsage(Arith.mul(Arith.div(used, total, 4), 100.0D));
/* 207 */       this.sysFiles.add(sysFile);
/*     */     } 
/*     */   }
/*     */   
/*     */   public String convertFileSize(long size) {
/* 219 */     long kb = 1024L;
/* 220 */     long mb = kb * 1024L;
/* 221 */     long gb = mb * 1024L;
/* 222 */     if (size >= gb)
/* 224 */       return String.format("%.1f GB", new Object[] { Float.valueOf((float)size / (float)gb) }); 
/* 226 */     if (size >= mb) {
/* 228 */       float f = (float)size / (float)mb;
/* 229 */       return String.format((f > 100.0F) ? "%.0f MB" : "%.1f MB", new Object[] { Float.valueOf(f) });
/*     */     } 
/* 231 */     if (size >= kb) {
/* 233 */       float f = (float)size / (float)kb;
/* 234 */       return String.format((f > 100.0F) ? "%.0f KB" : "%.1f KB", new Object[] { Float.valueOf(f) });
/*     */     } 
/* 238 */     return String.format("%d B", new Object[] { Long.valueOf(size) });
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\web\domain\Server.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */